#!/usr/bin/perl -w   

use strict;

use DBI;
use Time::localtime ;
use Time::Local ;
use CGI qw(:standard);
use MIME::Lite; 

my ($res_db,@res_db,@res_db2,$dbh);
my ($studentid,$folder,$comments,$sth,$sth2,$id,$i,$val,$passw2,$siponline,$vid);
my ($email,$passw,$SUBJECT,$msg,$email2,$email3,$msg2,$inconnect,$peerid,$path,$outstring,$siponline,$caller,$sipstatus,$callin,$callout);
my ($noregistration);
#$dbh=DBI->connect("dbi:mysql:dbname=sms:host=localhost","ddumper","ddumper");
$dbh=DBI->connect("dbi:mysql:dbname=ss77ru:host=localhost","ss77ru","662720");
	
$sth=$dbh->do("set character_set_results='cp1251'");
$sth=$dbh->do("set character_set_client='cp1251'");
$sth=$dbh->do("set collation_connection='cp1251_general_ci'"); 


$caller=param("caller");


print "Content-Type: text/html; charset=Windows-1251\n\n";



	    print qq
        	{
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Mizu WebPhone JavaScript API and the webphonetojs callback</title>
<style type="text/css" media="all">\@import "/home/trening/data/public_html/mizu/style.css";</style>
</head>

<body>




<applet
  archive  = "/home/trening/data/public_html/mizu/webphone.jar"
  codebase = "."
  code     = "webphone.webphone.class"
  name     = "webphone"
  width    = "1"
  height   = "1"
  hspace   = "0"
  vspace   = "0"
  align    = "middle"
  MAYSCRIPT
>

<param name=  "mayscript" value="yes">
<param name=  "scriptable" value="true">
<param name = "serveraddress" value = "yes77.ru">
<param name = "applet_size_width" value = "1">
<param name = "applet_size_height" value = "1">
<param name = "jsscriptevent" value = "1">
<param name = "use_stun" value = "0">

<param name = "language" value = "ru">


<param name = "use_gsm" value = "3">
<param name = "use_ilbc" value = "0">
<param name = "use_pcmu" value = "0">
<param name = "use_speex" value = "0">
<param name = "use_speexwb" value = "0">
<param name = "use_speexuwb" value = "0">
<param name = "use_g729" value = "0">
<param name = "use_pcma" value = "0">




<b>Java is not enabled.</b>

</applet>



<SCRIPT LANGUAGE="javascript">

   var applethandle = null;
   var webphoneStatus = null;


  function displaystatus(statustr)
  {
      if (webphoneStatus == null) {
           try{ webphoneStatus =  document.getElementById('webphoneStatus');  } catch (e) { }    
        }
     try{ webphoneStatus.innerHTML = statustr+'<br />'; } catch (e) { }  
  }


function displayEvent(evStr)	//	display event massages
{
	var displayEvent = document.getElementById('displayEvent');
	displayEvent.innerHTML = evStr;
	clearTimeout (eventAlertTimerId);
	eventAlertTimerId = setTimeout ( "hideEvent()", eventDisplayTime);
}





  function initcheck()
  {                 
        if (applethandle == null) 
        {                            
            displaystatus('JAVASCRIPT SAY: webphone init');

            try{ applethandle =  document.getElementById('webphone'); } catch (e) { }  
                
            if(applethandle == null)
            {
              var applets = null;            
              try{
              applets = document.applets; 

              // Needed for FireFox
              if (applets.length == 0) {
                  applets = document.getElementsByTagName("object");
              }
              if (applets.length == 0) {
                  applets = document.getElementsByTagName("applet");
              }

              //Find the active applet object
              for (var i = 0; i < applets.length; ++i) {
                  try {
                    if (typeof (applets[i].API_Call) != "undefined") {
                        applethandle = applets[i];
                        break;
                     }
                  } catch (e) { }
              }
              } catch (e) { }  
         
              if (applethandle == null) try{ applethandle = document.applets[0];  } catch (e) { }  

              if (applethandle == null) {
                  displaystatus('JAVASCRIPT SAY: cannot get applet handle'); 
              }
            }

            if (applethandle != null) 
            {              
              // See if we're using the old Java Plug-In and the JNLPAppletLauncher
              try {
                 applethandle = applethandle.getSubApplet();
              } catch (e) {
                  // Using new-style applet -- ignore
              }
            }          
        }               
  }







   function webphonetojs(varr)
{
	var eventNotify = '' + varr;


//	if(!initcheck()) {	/*alert('Cannot find applet handle !');*/	return false;	}
	initcheck();
	var stat = applethandle.API_GetStatus(-2);	//	get general status
	var status = stat.replace(/\s+/g,'');






	
//	var chanelStat = applethandle.API_GetStatus(1);	//	get line status
//	var chanelStatus = chanelStat.replace(/\s+/g,'');

			var realStatus = '';
			if (status == 'RegisterFailed')
			{
				realStatus = '<span style="color:#ff0000;">'+status+'</span>';
			}else
			{
				realStatus = '<span style="color:#26b14a;">'+status+'</span>';
			}
			displaystatus(realStatus);
}




    function voipRegister(username, password)
    {
        initcheck();        
       document.applets[0].API_Register("", username, password);  	
    }

    function voipCall(number)
    {
        initcheck();        
        document.applets[0].API_Call(-1, number); 
    }

    function voipHangup()
    {
        
        initcheck();        
        document.applets[0].API_Hangup(-2); 	
    }
    

    function AudioDevices()
    {
        
        initcheck();        
        document.applets[0].API_AudioDevice(); 	
    }



</SCRIPT> 


<input type=button value='Register' onClick='voipRegister("2987","111")'>
<input type=button value='Call' onClick='voipCall("NUMBERTOCALL")'>
<input type=button value='Hangup' onClick='voipHangup()'>
<input type=button value='Audio Devices' onClick='AudioDevices()'>


<p></p> <p></p>
<h2>Status: </h2>
<div id="webphoneStatus"></div>    


</body>

</html>	        };
	   die();

  print end_html;



